package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/goodsrpc/goodsrpc"
)

type Goods struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(30);not null;comment:'稿件名称'"`
	Price   float64 `gorm:"type:decimal(10,2);not null;comment:'稿件价格'"`
	Stuck   int64   `gorm:"type:int(11);not null;comment:'稿件库存'"`
	Image   string  `gorm:"type:varchar(255);not null;comment:'稿件图片'"`
	ClassId int64   `gorm:"type:int(11);not null;comment:'分类ID'"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(&g).Error
}

func (g *Goods) FindGoods(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(&g).Error
}

func GoodsList(db *gorm.DB, list []*goodsrpc.GoodsList) []*goodsrpc.GoodsList {
	db.Debug().Model(&Goods{}).
		Select("goods.name as name",
			"goods.price as price",
			"goods.stuck as stuck",
			"goods.image as image",
			"goods.id as goods_id",
			"classes.id as class_id").
		Joins("left join classes on classes.id=goods.class_id").
		Find(&list)
	return list
}
